package com.huawei.M01.iterator;

import java.util.Arrays;

public class MoveZeroes {
//    public void moveZeroes(int[] nums) {
//        int leftIndex = 0;
//        int rightIndex = 0;
//        while (leftIndex < nums.length) {
//            while (leftIndex < nums.length && nums[leftIndex] != 0) {
//                leftIndex++;
//            }
//            if (rightIndex < leftIndex) {
//                rightIndex = leftIndex + 1;
//            }
//            while (rightIndex < nums.length && nums[rightIndex] == 0) {
//                rightIndex++;
//            }
//            if (rightIndex >= nums.length) {
//                break;
//            }
//            nums[leftIndex] = nums[rightIndex];
//            nums[rightIndex] = 0;
//            rightIndex++;
//            leftIndex++;
//        }
//    }

    public void moveZeroes(int[] nums) {
        int leftIndex = 0;
        int rightIndex = 0;
        while (rightIndex < nums.length) {
            while (leftIndex < nums.length && nums[leftIndex] != 0) {
                leftIndex++;
            }
            if (rightIndex < leftIndex) {
                rightIndex = leftIndex + 1;
            }
            while (rightIndex < nums.length && nums[rightIndex] == 0) {
                rightIndex++;
            }
            if (rightIndex >= nums.length) {
                break;
            }
            nums[leftIndex] = nums[rightIndex];
            rightIndex++;
            leftIndex++;
        }
    }

    public static void main(String[] args) {
        MoveZeroes moveZeroes = new MoveZeroes();
        int[] nums = new int[]{0,1,0,3,12};
        moveZeroes.moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }
}
